﻿@model Site

@{
    Stack<Site> parents = new Stack<Site>();
    var parent = Model.Parent.AsActual();
    while (parent != null)
    {
        parents.Push(parent);
        parent = parent.Parent.AsActual();
    }
    var rootSite = parents.Count > 0 ? parents.Peek() : Model;
    var childSites = ServiceFactory.SiteManager.ChildSites(Model);

    var hasTree = parents.Count > 0 || childSites.Count() > 0;
}
@helper RenderParent(Stack<Site> parents)
{
    var parent = parents.Pop();
    <li>
        @Html.ActionLink(string.IsNullOrEmpty(parent.DisplayName) ? parent.Name : parent.DisplayName, "SiteMap", new { Area = "Sites", controller = "Home", siteName = parent.FullName, repositoryName = parent.AsActual().Repository })
        <ul>
            @if (parents.Count > 0)
            {
                @RenderParent(parents)
            }
            else
            {
                @RenderCurrent(Model)
            }
        </ul>
    </li>
}
@helper RenderChildSite(Site site)
{
    <li>
        @Html.ActionLink(string.IsNullOrEmpty(site.DisplayName) ? site.Name : site.DisplayName, "SiteMap", new { Area = "Sites", controller = "Home", siteName = site.FullName, repositoryName = site.AsActual().Repository })
        <ul>
            @foreach (var child in ServiceFactory.SiteManager.ChildSites(site))
            {
                @RenderChildSite(child.AsActual())
            }
        </ul>
    </li>
}
@helper RenderCurrent(Site site)
{
    <li class='active'>
        @Html.ActionLink(string.IsNullOrEmpty(site.DisplayName) ? site.Name : site.DisplayName, "SiteMap", new { Area = "Sites", controller = "Home", siteName = site.FullName, repositoryName = site.AsActual().Repository })


        <ul>
            @foreach (var child in ServiceFactory.SiteManager.ChildSites(site))
            {
                @RenderChildSite(child.AsActual())
            }
        </ul>
    </li>
}
@helper RenderSiteTree(Site site, Site current)
{
    <li @(site == current ? "class=active" : "")>
        @Html.ActionLink(string.IsNullOrEmpty(site.DisplayName) ? site.Name : site.DisplayName, "SiteMap", new { Area = "Sites", controller = "Home", siteName = site.FullName, repositoryName = site.AsActual().Repository })

        <ul>
            @foreach (var child in ServiceFactory.SiteManager.ChildSites(site))
            {
                @RenderSiteTree(child.AsActual(), current)
            }
        </ul>
    </li>
}

<div class="switcher @(hasTree ? "active" : "")">
    <span class="current">
        <span>@(string.IsNullOrEmpty(Site.Current.DisplayName) ? Site.Current.Name : Site.Current.DisplayName)</span>
        @Html.IconImage("arrow switch")
    </span>
    <ul class="list">
        @*@if (parents.Count > 0)
            {
                @RenderParent(parents)
            }
            else
            {
                @RenderCurrent(Model)
            }*@
        @RenderSiteTree(rootSite, Model)
    </ul>

</div>

